package com.jdd.dao.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdd.common.system.vo.LoginUser;
import com.jdd.common.util.LoginUserUtils;
import com.jdd.dao.entity.SysDepartArea;
import com.jdd.dao.entity.TProject;
import com.jdd.dao.mapper.SysDepartAreaMapper;
import com.jdd.dao.mapper.TProjectMapper;
import com.jdd.dao.service.ISysDepartAreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.dao.vo.SysAreaTreeModel;
import com.jdd.modules.system.mapper.SysUserMapper;
import com.jdd.modules.system.util.FindsSysAreaChildrenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ls
 * @since 2022-09-23
 */
@Slf4j
@Service
public class SysDepartDepartAreaServiceImpl extends ServiceImpl<SysDepartAreaMapper, SysDepartArea> implements ISysDepartAreaService {

    @Resource
    private SysDepartAreaMapper sysDepartAreaMapper;

    @Resource
    private TProjectMapper tProjectMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysAreaTreeModel> queryTreeList() {
        LoginUser loginUser = LoginUserUtils.getLoginUser();
        if(StringUtils.isNotBlank(loginUser.getOrgCode()))
        {
            LambdaQueryWrapper<SysDepartArea> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysDepartArea::getSysProjectCode,loginUser.getOrgCode());
            List<SysDepartArea> sysAreas = sysDepartAreaMapper.selectList(queryWrapper);
            List<String> areaIds = sysAreas.stream().map(SysDepartArea::getId).collect(Collectors.toList());
            List<TProject> tProjects = new ArrayList<>();
            if(CollUtil.isNotEmpty(areaIds))
            {
                LambdaQueryWrapper<TProject> query = new LambdaQueryWrapper<TProject>();
                query.in(TProject::getSysAreaId,areaIds);
                tProjects = tProjectMapper.selectList(query);
            }
            return this.wrapTreeDataToTreeList(sysAreas,tProjects);
        }else {
            List<SysDepartArea> sysAreas = sysDepartAreaMapper.selectList(new QueryWrapper<>());
            List<TProject> tProjects =  tProjectMapper.selectList(new QueryWrapper<>());
            return this.wrapTreeDataToTreeList(sysAreas,tProjects);
        }

    }

    @Override
    public boolean removeBind(String id) {
        return sysDepartAreaMapper.removeBind(id);
    }

    public  List<SysAreaTreeModel> wrapTreeDataToTreeList(List<SysDepartArea> sysAreas, List<TProject> tProjects) {
        List<SysAreaTreeModel> records = new ArrayList<>();
        for (int i = 0; i <sysAreas.size(); i++) {
            SysDepartArea sysDepartArea = sysAreas.get(i);
            SysAreaTreeModel model = new SysAreaTreeModel(sysDepartArea);
            model.setBindUsers(sysUserMapper.getSysUserByDepartId(sysDepartArea.getId()));
            if(model.getParentId()!=null){
                SysDepartArea area = this.getById(model.getParentId());
                if (area != null) {
                    model.setParentName(area.getName());
                }
            }
            records.add(model);
        }
        for (int i = 0; i < tProjects.size(); i++) {
            TProject tProject = tProjects.get(i);
            SysAreaTreeModel model = new SysAreaTreeModel(tProject);
            model.setBindUsers(sysUserMapper.getSysUserByDepartId(tProject.getId()));
            if(model.getParentId()!=null){
                SysDepartArea area = this.getById(model.getParentId());
                model.setParentName(area.getName());
            }
            records.add(model);
        }
        List<SysAreaTreeModel> tree = FindsSysAreaChildrenUtil.findChildren(records);
        FindsSysAreaChildrenUtil.setEmptyChildrenAsNull(tree);
        return tree;
    }


}
